﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using RBAC.ORM;//请先添加引用
namespace RBAC.ORM.DAL
{
    /// <summary>
    /// 数据访问类SLogin。
    /// </summary>
    public class SLogin
    {
        public SLogin()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string userID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SLogin");
            strSql.Append(" where userID=@userID ");
            SqlParameter[] parameters = {
					new SqlParameter("@userID", SqlDbType.NVarChar,50)};
            parameters[0].Value = userID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(RBAC.ORM.Model.SLogin model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SLogin(");
            strSql.Append("userID,password,question,answer,lastlogintime,lastloginip,logincounts,lastUpdater,lastUpdateTime,enable)");
            strSql.Append(" values (");
            strSql.Append("@userID,@password,@question,@answer,@lastlogintime,@lastloginip,@logincounts,@lastUpdater,@lastUpdateTime,@enable)");
            SqlParameter[] parameters = {
					new SqlParameter("@userID", SqlDbType.NVarChar,50),
					new SqlParameter("@password", SqlDbType.NVarChar,50),
					new SqlParameter("@question", SqlDbType.NVarChar,200),
					new SqlParameter("@answer", SqlDbType.NVarChar,200),
					new SqlParameter("@lastlogintime", SqlDbType.DateTime),
					new SqlParameter("@lastloginip", SqlDbType.NVarChar,50),
					new SqlParameter("@logincounts", SqlDbType.Decimal,9),
					new SqlParameter("@lastUpdater", SqlDbType.NVarChar,50),
					new SqlParameter("@lastUpdateTime", SqlDbType.DateTime),
					new SqlParameter("@enable", SqlDbType.SmallInt,2)};
            parameters[0].Value = model.userID;
            parameters[1].Value = model.password;
            parameters[2].Value = model.question;
            parameters[3].Value = model.answer;
            parameters[4].Value = model.lastlogintime;
            parameters[5].Value = model.lastloginip;
            parameters[6].Value = model.logincounts;
            parameters[7].Value = model.lastUpdater;
            parameters[8].Value = model.lastUpdateTime;
            parameters[9].Value = model.enable;

            try
            {
                DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(RBAC.ORM.Model.SLogin model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SLogin set ");
            strSql.Append("password=@password,");
            strSql.Append("question=@question,");
            strSql.Append("answer=@answer,");
            strSql.Append("lastlogintime=@lastlogintime,");
            strSql.Append("lastloginip=@lastloginip,");
            strSql.Append("logincounts=@logincounts,");
            strSql.Append("lastUpdater=@lastUpdater,");
            strSql.Append("lastUpdateTime=@lastUpdateTime,");
            strSql.Append("enable=@enable");
            strSql.Append(" where userID=@userID ");
            SqlParameter[] parameters = {
					new SqlParameter("@userID", SqlDbType.NVarChar,50),
					new SqlParameter("@password", SqlDbType.NVarChar,50),
					new SqlParameter("@question", SqlDbType.NVarChar,200),
					new SqlParameter("@answer", SqlDbType.NVarChar,200),
					new SqlParameter("@lastlogintime", SqlDbType.DateTime),
					new SqlParameter("@lastloginip", SqlDbType.NVarChar,50),
					new SqlParameter("@logincounts", SqlDbType.Decimal,9),
					new SqlParameter("@lastUpdater", SqlDbType.NVarChar,50),
					new SqlParameter("@lastUpdateTime", SqlDbType.DateTime),
					new SqlParameter("@enable", SqlDbType.SmallInt,2)};
            parameters[0].Value = model.userID;
            parameters[1].Value = model.password;
            parameters[2].Value = model.question;
            parameters[3].Value = model.answer;
            parameters[4].Value = model.lastlogintime;
            parameters[5].Value = model.lastloginip;
            parameters[6].Value = model.logincounts;
            parameters[7].Value = model.lastUpdater;
            parameters[8].Value = model.lastUpdateTime;
            parameters[9].Value = model.enable;

            try
            {
                DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string userID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SLogin ");
            strSql.Append(" where userID=@userID ");
            SqlParameter[] parameters = {
					new SqlParameter("@userID", SqlDbType.NVarChar,50)};
            parameters[0].Value = userID;

            try
            {
                DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RBAC.ORM.Model.SLogin GetModel(string userID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 userID,password,question,answer,lastlogintime,lastloginip,logincounts,lastUpdater,lastUpdateTime,enable from SLogin ");
            strSql.Append(" where userID=@userID ");
            SqlParameter[] parameters = {
					new SqlParameter("@userID", SqlDbType.NVarChar,50)};
            parameters[0].Value = userID;

            RBAC.ORM.Model.SLogin model = new RBAC.ORM.Model.SLogin();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.userID = ds.Tables[0].Rows[0]["userID"].ToString();
                model.password = ds.Tables[0].Rows[0]["password"].ToString();
                model.question = ds.Tables[0].Rows[0]["question"].ToString();
                model.answer = ds.Tables[0].Rows[0]["answer"].ToString();
                if (ds.Tables[0].Rows[0]["lastlogintime"].ToString() != "")
                {
                    model.lastlogintime = DateTime.Parse(ds.Tables[0].Rows[0]["lastlogintime"].ToString());
                }
                model.lastloginip = ds.Tables[0].Rows[0]["lastloginip"].ToString();
                if (ds.Tables[0].Rows[0]["logincounts"].ToString() != "")
                {
                    model.logincounts = decimal.Parse(ds.Tables[0].Rows[0]["logincounts"].ToString());
                }
                model.lastUpdater = ds.Tables[0].Rows[0]["lastUpdater"].ToString();
                if (ds.Tables[0].Rows[0]["lastUpdateTime"].ToString() != "")
                {
                    model.lastUpdateTime = DateTime.Parse(ds.Tables[0].Rows[0]["lastUpdateTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["enable"].ToString() != "")
                {
                    model.enable = int.Parse(ds.Tables[0].Rows[0]["enable"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select userID,password,question,answer,lastlogintime,lastloginip,logincounts,lastUpdater,lastUpdateTime,enable ");
            strSql.Append(" FROM SLogin ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" userID,password,question,answer,lastlogintime,lastloginip,logincounts,lastUpdater,lastUpdateTime,enable ");
            strSql.Append(" FROM SLogin ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "SLogin";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}


